package com.css.apps.task.job.impl;

import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import com.css.apps.task.job.JobTemplate;
import com.css.util.StringHelper;

public class UrlJob extends JobTemplate {
	
	private static Log log = LogFactory.getLog(UrlJob.class);

	public List<?> beforeJob() {
		return null;
	}

	@Override
	protected void doJob(Object obj) {
		CloseableHttpClient client = null;
		String url = getExePara("url");
		if (StringHelper.isNotEmpty(url)) {
			client =  HttpClientBuilder.create().build();
			CloseableHttpResponse response = null;
			try {
				HttpPost request = new HttpPost(url);
				int timeoutSecond = 5 * 1000;
				RequestConfig requestConfig = RequestConfig.custom()
					      .setConnectionRequestTimeout(timeoutSecond)
					      .setConnectTimeout(timeoutSecond)
					      .setSocketTimeout(timeoutSecond).build();
				request.setConfig(requestConfig);
				response = client.execute(request);
				int statusCode = response.getStatusLine().getStatusCode();
				if (HttpStatus.SC_OK != statusCode) {
					log.error("Method failed: " + response.getStatusLine());
				}
			} catch (Exception e) {
				System.out.println("===============================" + url);
				e.printStackTrace();
			} finally {
				if (response != null) {
					try {
						response.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (client != null) {
					try {
						client.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

}
